package org.dftproject.genesis.ui.pages.pedigree.tree;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.draw2d.Animation;
import org.eclipse.draw2d.Animator;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.LayoutAnimator;
import org.eclipse.draw2d.LayoutListener;
import org.eclipse.draw2d.geometry.Rectangle;

/**
 * Animates the layout of a figure's children. The animator will capture the
 * effects of a layout manager, and then play back the placement of children
 * using linear interpolation for each child's start and end locations.
 * <P>
 * To use an animator, hook it as a layout listener for the figure whose layout
 * is to be animated, by calling
 * {@link IFigure#addLayoutListener(LayoutListener)}. It is not necessary to
 * have an animator for every figure in a composition that is undergoing
 * animation. For example, if a figure without an animator moves during the
 * animation, it will continue to move and layout its children normally during
 * each step of the animation.
 * <P>
 * Animator must be used in conjunction with layouts. If figures are placed
 * manually using <code>setBounds()</code>, the animator may not be able to
 * track and playback the changes that occur.
 * 
 * @since 3.2
 */
public class PedigreeAnimator extends LayoutAnimator {

	static final PedigreeAnimator INSTANCE = new PedigreeAnimator();

	/**
	 * Constructs a new Animator. The default instance ({@link #getDefault()})
	 * can be used on all figures being animated.
	 * 
	 * @since 3.2
	 */
	protected PedigreeAnimator() {
	}

	/**
	 * Returns an object encapsulating the placement of children in a container.
	 * This method is called to capture both the initial and final states.
	 * @param container the container figure
	 * @return the current state
	 * @since 3.2
	 */
	@Override
	protected Object getCurrentState(IFigure container) {
		Map locations = new HashMap();
		List children = container.getChildren();
		IFigure child;
		for (int i = 0; i < children.size(); i++) {
			child = (IFigure) children.get(i);
			locations.put(child, child.getBounds().getCopy());
		}
		return locations;
	}

	/**
	 * Returns the default instance.
	 * @return the default instance
	 * @since 3.2
	 */
	public static PedigreeAnimator getDefault() {
		return INSTANCE;
	}

	/**
	 * Plays back the animated layout.
	 * @see Animator#playback(IFigure)
	 */
	@Override
	protected boolean playback(IFigure container) {
		Map initial = (Map) Animation.getInitialState(this, container);
		Map ending = (Map) Animation.getFinalState(this, container);
		if (initial == null)
			return false;
		List children = container.getChildren();

		float progress = Animation.getProgress();
		float ssergorp = 1 - progress;

		Rectangle rect1, rect2;

		for (int i = 0; i < children.size(); i++) {
			IFigure child = (IFigure) children.get(i);
			rect1 = (Rectangle) initial.get(child);
			rect2 = (Rectangle) ending.get(child);

			if (rect1 == null || rect1.isEmpty()) {
				child.setBounds(rect2);
				if (child instanceof AlphaFigure) {
					((AlphaFigure) child).setAlpha((int) (255 * progress));
					child.repaint();
				}
			} else {
				child.setBounds(new Rectangle(Math.round(progress * rect2.x + ssergorp * rect1.x), Math.round(progress * rect2.y + ssergorp * rect1.y), Math.round(progress * rect2.width + ssergorp * rect1.width), Math.round(progress * rect2.height + ssergorp * rect1.height)));
			}
		}
		return true;
	}

}
